﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DEMO3.BUS;
using DEMO3.DTO;

namespace DEMO3.GUI
{
    public partial class Phim : UserControl
    {
        public Phim()
        {
            InitializeComponent();
        }

        private void Phim_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        private void LoadData()
        {
            cbxDinhDang.SelectedIndex = 0;
            PhimBUS bus = new PhimBUS();
            gc.DataSource = bus.Get();

            TheLoaiPhimBUS bus2 = new TheLoaiPhimBUS();
            cbxTheLoai.DataSource = bus2.Get();
            cbxTheLoai.DisplayMember = "TenTheLoai";
            cbxTheLoai.ValueMember = "MaTheLoai";
        }

        private void gv_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            if (gv.FocusedRowHandle != gv.RowCount && gv.RowCount != 0)
            {
                txtTenPhim.Text = gv.GetRowCellValue(gv.FocusedRowHandle, "TenPhim").ToString();
                txtNamSX.Text = gv.GetRowCellValue(gv.FocusedRowHandle, "NamSanXuat").ToString();
                txtNuocSX.Text = gv.GetRowCellValue(gv.FocusedRowHandle, "NuocSanXuat").ToString();
                txtHangPhim.Text = gv.GetRowCellValue(gv.FocusedRowHandle, "HangPhim").ToString();
                txtThoiLuong.Text = gv.GetRowCellValue(gv.FocusedRowHandle, "ThoiLuong").ToString();
                txtGioiThieu.Text = gv.GetRowCellValue(gv.FocusedRowHandle, "GioiThieu").ToString();
                txtPoster.Text = gv.GetRowCellValue(gv.FocusedRowHandle, "Poster").ToString();
                txtDSDienVien.Text = gv.GetRowCellValue(gv.FocusedRowHandle, "DanhSachDienVien").ToString();
                cbxDinhDang.SelectedItem = gv.GetRowCellValue(gv.FocusedRowHandle, "DinhDang").ToString();
                cbxTheLoai.SelectedValue = gv.GetRowCellValue(gv.FocusedRowHandle, "MaTheLoai").ToString();
                cbTinhTrang.Checked = bool.Parse(gv.GetRowCellValue(gv.FocusedRowHandle, "TrangThai").ToString());
            }
        }

        private void btThem_Click(object sender, EventArgs e)
        {
            PhimDTO dto = new PhimDTO();
            dto.TenPhim = txtTenPhim.Text;
            dto.NamSanXuat = txtNamSX.Text;
            dto.NuocSanXuat = txtNuocSX.Text;
            dto.HangPhim = txtHangPhim.Text;
            dto.Poster = txtPoster.Text;
            dto.GioiThieu = txtGioiThieu.Text;
            dto.ThoiLuong = int.Parse(txtThoiLuong.Text);
            dto.DinhDang = cbxDinhDang.SelectedItem.ToString();
            dto.DSDienVien = txtDSDienVien.Text;
            dto.TheLoai = cbxTheLoai.SelectedValue.ToString();
            dto.TrangThai = cbTinhTrang.Checked;

            PhimBUS bus = new PhimBUS();
            bus.Add(dto);
            
            LoadData();
        }

        private void btXoa_Click(object sender, EventArgs e)
        {
            PhimDTO dto = new PhimDTO();
            dto.MaPhim = int.Parse(gv.GetRowCellValue(gv.FocusedRowHandle, "MaPhim").ToString());

            PhimBUS bus = new PhimBUS();
            bus.Delete(dto);
            
            LoadData();
        }

        private void btSua_Click(object sender, EventArgs e)
        {
            PhimDTO dto = new PhimDTO();
            dto.MaPhim = int.Parse(gv.GetRowCellValue(gv.FocusedRowHandle, "MaPhim").ToString());
            dto.TenPhim = txtTenPhim.Text;
            dto.NamSanXuat = txtNamSX.Text;
            dto.NuocSanXuat = txtNuocSX.Text;
            dto.HangPhim = txtHangPhim.Text;
            dto.Poster = txtPoster.Text;
            dto.GioiThieu = txtGioiThieu.Text;
            dto.ThoiLuong = int.Parse(txtThoiLuong.Text);
            dto.DinhDang = cbxDinhDang.SelectedItem.ToString();
            dto.DSDienVien = txtDSDienVien.Text;
            dto.TheLoai = cbxTheLoai.SelectedValue.ToString();
            dto.TrangThai = cbTinhTrang.Checked;

            PhimBUS bus = new PhimBUS();
            bus.Update(dto);

            LoadData();
        }

    }
}
